package com.laihui.webview;

import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.laihui.activity.R;
import com.laihui.base.BaseActivity;
import com.laihui.view.TopBar;

/**
 * Created by Administrator on 2018/1/8.
 */

public class ActivityWebView  extends BaseActivity {
    WebView mWebview;
    WebSettings mWebSettings;
    TopBar mtitle;

    private String title,url;
    ProgressBar mProgressBar;



    @Override
    public void init() {
        mWebview = (WebView) findViewById(R.id.webView1);
        mProgressBar = (ProgressBar) findViewById(R.id.pb_progress);
        mtitle = (TopBar) findViewById(R.id.topbar);
        mtitle.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");

        mWebSettings = mWebview.getSettings();
        mtitle.setTitle(title);
        mWebview.loadUrl(url);


        //设置不用系统浏览器打开,直接显示在当前Webview
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //设置WebChromeClient类
        mWebview.setWebChromeClient(new WebChromeClient() {


            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                mtitle.setTitle(title);
            }
            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                try {
                    if (mProgressBar == null) {
                        return;
                    }
                    if (newProgress == 100) {
                        // 网页加载完成
                        mProgressBar.setVisibility(View.GONE);
                    } else {
                        // 加载中
                        mProgressBar.setProgress(newProgress);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onProgressChanged(view, newProgress);
            }
        });

    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebview.canGoBack()) {
            mWebview.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (mWebview != null) {
            mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebview.clearHistory();

            ((ViewGroup) mWebview.getParent()).removeView(mWebview);
            mWebview.destroy();
            mWebview = null;
        }
        super.onDestroy();
    }
}
